package fate.rent.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import fate.car.pojo.ACar;
import fate.common.util.PageInfoUtil;
import fate.rent.dao.IAAdminsDao;
import fate.rent.pojo.AAdmins;
import fate.rent.pojo.AUsers;
import fate.rent.service.IUsersService;

/**
 * service接口的实现类
 * 
 * @author Administrator
 *
 */
@Service("userService")
public class IUsersServiceImpl implements IUsersService
{
	@Resource
	private IAAdminsDao iAAdminsDao;
	// 管理员登录功能
	@Override
	public AUsers auserLoginService(Map<String, Object> condMap)
	{
		
		return this.iAAdminsDao.auserslogin(condMap);
	}
	//添加客户
	@Override
	public JSONObject saveListUsersService(AUsers aUsers)
	{
		JSONObject dataJSON = new JSONObject();
		JSONObject resultJSON = new JSONObject();
		int res = this.iAAdminsDao.saveUsers(aUsers);
		if(res > 0){
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			/* 存储的data也是一个json对象 */

			/*
			 * 成功 
			 *  res:执行此SQL语句对数据库影响的条数;
			 * */
			dataJSON.put("effect", res);
			dataJSON.put("id", aUsers.getId());
			resultJSON.put("data", dataJSON);
			
		}else{
			/*失敗*/
			resultJSON.put("code", "1");
			resultJSON.put("info", "失敗");
		}
		// TODO Auto-generated method stub
		return resultJSON;
	}
	//查询客户信息
	@Override
	public List<AUsers> ListUsersService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 为关键字模糊查询,加%号 */
		if(condMap.get("name") != null)
		{
			condMap.put("name", "%" + condMap.get("name") +"%");
		}
		if(pageInfoUtil !=null)
		{
			/* 分页 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AUsers> userList = this.iAAdminsDao.listUser(condMap);
			/* 设置一下总记录数 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return userList ;
		}
		return this.iAAdminsDao.listUser(condMap);
	}
	//添加用户
	@Override
	public JSONObject saveListAdminsService(AAdmins aAdmins)
	{
		JSONObject dataJSON = new JSONObject();
		JSONObject resultJSON = new JSONObject();
		int res = this.iAAdminsDao.saveAdmins(aAdmins);
		if(res > 0){
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			/* 存储的data也是一个json对象 */

			/*
			 * 成功 
			 *  res:执行此SQL语句对数据库影响的条数;
			 * */
			dataJSON.put("effect", res);
			dataJSON.put("id", aAdmins.getId());
			resultJSON.put("data", dataJSON);
			
		}else{
			/*失敗*/
			resultJSON.put("code", "1");
			resultJSON.put("info", "失敗");
		}
		// TODO Auto-generated method stub
		return resultJSON;
	}
	
	//查询用户信息
	@Override
	public List<AAdmins> ListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 为关键字模糊查询,加%号 */
		if(condMap.get("name") != null)
		{
			condMap.put("name", "%" + condMap.get("name") +"%");
		}
		if(pageInfoUtil !=null)
		{
			/* 分页 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AAdmins> adminsList = this.iAAdminsDao.listAdmins(condMap);
			/* 设置一下总记录数 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return adminsList ;
		}
		return this.iAAdminsDao.listAdmins(condMap);
	}
	//删除用户
	@Override
	public int deleteAdmins(Map<String, Object> condMap)
	{
		int i = this.iAAdminsDao.deleteA(condMap);
		return i;
	}
	//删除客户
	@Override
	public int deleteUsers(Map<String, Object> condMap)
	{
		int i = this.iAAdminsDao.deleteU(condMap);
		return i;
	}

}
